import { onUnmounted, ref, type Ref } from 'vue'
import { cancelOrder, deleteOrder, followOrUnFollow } from '@/services/consult'
import type { ConsultOrderItem, Doctor, FollowType } from '@/types/consult'
import { getPrescriptionDetail } from '@/services/consult'
import {
  showFailToast,
  showImagePreview,
  showSuccessToast,
  showToast,
  type FormInstance,
} from 'vant'
import { OrderType } from '@/enums'
import type { OrderDetail } from '@/types/order'
import { getOrderDetail } from '@/services/order'
import { sendMobileCode } from '@/services/user'
import type { CodeType } from '@/types/user'
export const useFollow = (type: FollowType = 'doc') => {
  const loading = ref(false)
  const follow = async (item: { id: string; likeFlag: 0 | 1 }) => {
    loading.value = true
    try {
      await followOrUnFollow(item.id, type)
      item.likeFlag = item.likeFlag === 1 ? 0 : 1
    } finally {
      loading.value = false
    }
  }
  return { follow, loading }
}
export const useShowPre = () => {
  const onPreviewPre = async (id?: string) => {
    if (id) {
      const res = await getPrescriptionDetail(id)
      showImagePreview([res.data.url])
    }
  }
  return { onPreviewPre }
}
export const useCancelOrder = () => {
  const loading = ref(false)
  const onCancelOrder = async (item: ConsultOrderItem) => {
    try {
      loading.value = true
      await cancelOrder(item.id)
      item.statusValue = '已取消'
      item.status = OrderType.ConsultCancel
      showSuccessToast('取消成功')
    } catch (error) {
      showFailToast('取消失败')
    } finally {
      loading.value = false
    }
  }
  return { onCancelOrder, loading }
}
export const useDeleteOrder = (cb: () => void) => {
  const deleteLoading = ref(false)

  const onDeleteOrder = async (item: ConsultOrderItem) => {
    try {
      deleteLoading.value = true
      await deleteOrder(item.id)
      showSuccessToast('删除成功')
      cb && cb()
    } catch (error) {
      showFailToast('删除失败')
    } finally {
      deleteLoading.value = false
    }
  }
  return { onDeleteOrder, deleteLoading }
}
export const useOrderDetail = (id: string) => {
  const order = ref<OrderDetail>()
  const loading = ref(false)
  const handleGetOrderDetail = async () => {
    try {
      loading.value = true
      const res = await getOrderDetail(id)
      order.value = res.data
    } catch (error) {
    } finally {
      loading.value = false
    }
  }
  return { order, loading, handleGetOrderDetail }
}
export const useMobileCode = (mobile: Ref<string>, type: CodeType = 'login') => {
  const time = ref(0)
  const form = ref<FormInstance>()
  let timer: number
  const onSend = async () => {
    if (time.value > 0) return
    await form.value?.validate('mobile')
    await sendMobileCode(mobile.value, type)
    showToast('验证码发送成功')
    time.value = 60
    if (timer) clearInterval(timer)
    timer = setInterval(() => {
      time.value--
      if (time.value <= 0) {
        clearInterval(timer)
      }
    }, 1000)
  }
  onUnmounted(() => {
    clearInterval(timer)
  })
  return { time, form, onSend }
}
